@extends("ogreencard/layout/layout")
@section("h1","套餐管理")
@section("h2","套餐添加、编辑")
@section("content")
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form">
            {{csrf_field()}}
            <input type="hidden" name="id" value="{{$type->pack_id}}">

            <div class="layui-form-item">
                <label for="L_username" class="layui-form-label"><span class="x-red">*</span>套餐名称</label>
                <div class="layui-input-inline">
                    <input type="text"  name="pack_name" required="" lay-verify="name" autocomplete="off" class="layui-input" value="{{$type->pack_name}}">
                </div>
            </div>

            <div class="layui-form-item">
                <label for="L_username" class="layui-form-label"><span class="x-red">*</span>套餐类型</label>
                <div class="layui-input-inline">
                    <select name="pack_type" id="" class="layui-select">
                        @foreach($packTypes as $t)
                            <option value="{{$t->pack_type_id}}" @if($type->pack_type == $t->pack_type_id) selected @endif>{{$t->pack_type_name}}</option>
                        @endforeach
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label for="L_username" class="layui-form-label"><span class="x-red">*</span>兑换积分</label>
                <div class="layui-input-inline">
                    <input type="text"  name="pack_price" required="" lay-verify="pack_price" autocomplete="off" class="layui-input" value="{{$type->pack_price}}">
                </div>
            </div>

            <div class="layui-form-item">
                <label for="L_username" class="layui-form-label"><span class="x-red">*</span>套餐价值</label>
                <div class="layui-input-inline">
                    <input type="text"  name="pack_money" required="" lay-verify="pack_money" autocomplete="off" class="layui-input" value="{{$type->pack_money}}">
                </div>
            </div>

            <div class="layui-form-item layui-form-text">
                <label for="desc" class="layui-form-label"><span class="x-red">*</span>套餐内容</label>
                <div class="layui-input-block">
                    <textarea placeholder="请输入内容" id="desc" name="pack_content" lay-verify="pack_content" class="layui-textarea">{{$type->pack_content}}</textarea>
                </div>
            </div>

            <div class="layui-form-item">
                <label for="L_repass" class="layui-form-label"></label>
                <button class="layui-btn" lay-filter="add" lay-submit="">提交</button>
            </div>
        </form>
    </div>
</div>
<script>
    const source = "{{$from}}";
    console.log('from---',source);
    layui.use(['form', 'layer'],
        function () {
            $ = layui.jquery;
            var form = layui.form,
                layer = layui.layer;

            //自定义验证规则
            form.verify({
                pack_name: function (value) {
                    if (value.length < 1) {
                        return '填写套餐名称';
                    }
                },
                pack_price:function(value){
                    if(value.length < 1){
                        return "填写金额";
                    }
                }
            });

            //监听提交
            form.on('submit(add)', function (data)
            {
                console.log(data);
                //发异步，把数据提交给php
                layui.jquery.post({
                    url:"./cardTypeEdit",
                    data:data.field,
                    success:function(res){
                        console.log(res);
                        if(res.code != 200) {

                    		return layer.msg(res.msg,{icon:7},function(){})
                        }

                        if(source == 'order'){
                            parent.window.set_cache();
                        }

                        layer.alert("操作成功",{icon:6},function(){

                            parent.window.location.reload();

                            // 获得frame索引
                            //var index = parent.layer.getFrameIndex(window.name);

                            //关闭当前frame
                            //parent.layer.close(index);
                        })
                    }
                });
                return false;
            });

        });
</script>
@endsection